<?php
/**
 * 临时需求
 */
namespace Wap\Controller;

use Common\Model\FormModel;
use Common\Model\RedpackModel;

use Common\Model\UserModel;
use Common\Model\UtilsModel;
use Wap\BaseController;

class TmpController extends BaseController {


    /**
     * 生日会报名
     */
    public function index(){
        $uid=session('uid')+0;
        if(IS_AJAX){
            $data=array();
            $data['addTime']    = time();
            $data['uid']        = $uid;
            $data['parentName'] = I('post.realname');
            $data['phone'] = I('post.phone');
            $data['code'] = I('post.code',null);
            $data['address'] = I('post.address');
            if(!$data['phone']){
                $this->ajaxReturn(array('status'=>1,'msg'=>'家长的联系电话是必填项唷！'));
            }

            UserModel::getInstance()->startTrans();

            //////////////////////////////////////////////////////
            $bindPhone=session('bindPhone');
            if(!$bindPhone) {//如果没有绑定手机
                $uid = session("uid");
                $redata = UserModel::bindUserPassword($uid, $data['phone'], $data['code']);
                if ($redata['status'] != 0) {
                    UserModel::getInstance()->rollback();
                    $this->ajaxReturn($redata);
                }

                $redata['firstBindPhone'] = 0;
                if ($redata['status'] == 0) {
                    self::resetUserSessions($redata['data']);
                    $switch = UserModel::isUserInfoPerfect($uid, OPT_BIND_PHONE);
                    if ($switch) {
                        //手机绑定加积分100
                        UserModel::addUserValByAction($uid, OPT_BIND_PHONE);
                        $redata['firstBindPhone'] = 1;
                    }
                }
            }
            ////////////////////////////////////////////////////


            $nickname=I('post.nickname');
            $sex=I('post.sex');
            $birthday=I('post.birthday');

            for($i=0;$i<=2;$i++) {
                if (array_key_exists($i, $birthday)) {
                    if(!$nickname[$i]){
                        UserModel::getInstance()->rollback();
                        $this->ajaxReturn(array('status'=>1,'msg'=>'宝宝的昵称是必填项唷！'));
                    }

                    if(!$sex[$i]){
                        UserModel::getInstance()->rollback();
                        $this->ajaxReturn(array('status'=>1,'msg'=>'宝宝的性别是必填项唷！'));
                    }

                    if(!$birthday[$i]){
                        UserModel::getInstance()->rollback();
                        $this->ajaxReturn(array('status'=>1,'msg'=>'宝宝的生日是必填项唷！'));
                    }
                    $data['babySex' . ($i + 1)] = $sex[$i];
                    $data['babyName' . ($i + 1)] = $nickname[$i];
                    $data['babyBirthday' . ($i + 1)] = $birthday[$i];
                }
            }
            $M=M('tmp_babyinfo',null,DB_MAIN_CFG);
            $M->add($data,null,true);
            UserModel::getInstance()->commit();
            $this->ajaxReturn(array('status'=>0,'msg'=>'信息提交成功,<br />后续如有修改请联系客服'));
        }
        else{

            if(!UserModel::checkFollow($uid)){
                $this->smarty->display("tmp/focus.html");
                die;
            }

            $M=M('tmp_babyinfo',null,DB_MAIN_CFG);
            $babyinfo=$M->where("uid={$uid}")->find();
            if($babyinfo){
                $this->smarty->assign('babyInfo',$babyinfo);
                $this->smarty->assign('title','信息查看');
            }else{
                $this->smarty->assign('title','信息填写');
            }

            $this->smarty->display("tmp/babyInfo.html");

        }
    }


    public function pet(){
        $uid=session('uid')+0;
        if(IS_AJAX){
            $data=array();
            $data['addTime']    = time();
            $data['uid']        = $uid;
            $data['realname'] = I('post.realname');
            $data['phone'] = I('post.phone');

            if(!$data['phone']){
                $this->ajaxReturn(array('msg'=>'联系电话是必填项唷！'));
            }


            $data['people']=I('post.people');
            $data['babyNum']=I('post.babyNum');
            $data['job']=I('post.job');
            $data['area']=I('post.area');

            $data['pet']=I('post.pet');
            $data['say'] = I('post.say');
            $M=M('tmp_pet',null,DB_MAIN_CFG);
            $M->add($data,null,true);
            $this->ajaxReturn(array('msg'=>'信息提交成功'));

        }
        else{
            if(!UserModel::checkFollow($uid)){
                $this->smarty->display("tmp/focus.html");
                die;
            }

            $M=M('tmp_pet',null,DB_MAIN_CFG);
            $babyinfo=$M->where("uid={$uid}")->find();
            if($babyinfo){
                $this->smarty->assign('babyInfo',$babyinfo);
            }
            $this->smarty->assign('title','宠物领养');
            $this->smarty->display("tmp/pet.html");

        }
    }



    public function  form(){
        $fid=I('get.fid',0,'intval');
        if(!$fid){
            $this->error('指定的表单错误！');
        }


        $uid=session('uid')+0;
        $form=FormModel::getFormInstance()->where("fid=".$fid)->find();
        if($form['mustFollow'] && !UserModel::checkFollow($uid)){
            $this->smarty->display("tmp/focus.html");
            die;
        }

        if(IS_AJAX){
            $fid=I('post.fid',0,'intval');
            $fields=explode(';',$form['content']);
            $valarr=array();
            foreach($fields as $key=>$one){
                list($fname,$ftypeParam,$must,$append)=explode('+',$one);
                list($ftype,$fparam)=explode(':',$ftypeParam);

                $val=I('post.f'.$key,'');
                if($must && $val==''){
                    $this->ajaxReturn(array('status'=>1,'msg'=>"{$fname} 是必填项，请完成填写"));
                }
                switch ($ftype){
                    case FormModel::FIELD_DATE_FUTURE:
                        if(strtotime($val)>=strtotime(date('Y-m-d'))){
                            $valarr[]=$val;
                        }else{
                            $this->ajaxReturn(array('status'=>1,'msg'=>"{$fname} 必须是今天或之后的日期"));
                        }
                        break;
                    case FormModel::FIELD_DATE_PAST:
                        if(strtotime($val)<=strtotime(date('Y-m-d'))){
                            $valarr[]=$val;
                        }else{
                            $this->ajaxReturn(array('status'=>1,'msg'=>"{$fname} 必须是今天或之前的日期"));
                        }
                        break;
                    case FormModel::FIELD_TIME:
                        $valarr[]=$val;
                        break;
                    case FormModel::FIELD_NUM:
                            if(is_numeric($val)){
                                $valarr[]=$val;
                            }else{
                                $this->ajaxReturn(array('status'=>1,'msg'=>"{$fname} 必须是数值"));
                            }
                        break;
                    case FormModel::FIELD_TEXT:
                        $valarr[]=$val;
                        break;
                    case FormModel::FIELD_LONGTEXT:
                        $valarr[]=$val;
                        break;
                    case FormModel::FIELD_SINGLE:
                        $valarr[]=$val;
                        break;
                    case FormModel::FIELD_MULTI:
                        $valarr[]=implode(',',I('post.f'.$key));
                        break;
                    case FormModel::FIELD_EMAIL:
                        if(filedPreMatch('email', $val)){
                            $valarr[]=$val;
                        }else{
                            $this->ajaxReturn(array('status'=>1,'msg'=>"{$fname} 不是合格的邮箱地址"));
                        }
                        break;
                    case FormModel::FIELD_PHONE:
                        if(filedPreMatch('phone', $val)){
                            $valarr[]=$val;
                        }else{
                            $this->ajaxReturn(array('status'=>1,'msg'=>"{$fname} 不是合格的手机号码"));
                        }
                        break;
                    case FormModel::FIELD_PHOTO:
                        break;
                    case FormModel::FIELD_AREA:
                        $valarr[]=$val;
                        break;
                }
            }
            $data=array(
                'fid'=>$fid,
                'uid'=>$uid,
                'data'=>implode(';',$valarr),
                'addTime'=>time()
            );
            FormModel::getDataInstance()->add($data,null,true);

            unset($fields);
            $this->ajaxReturn(array('status'=>0,'msg'=>'success'));

        }





        $data=FormModel::getDataInstance()->where("uid={$uid} and fid={$fid}")->find();
        $fields=explode(';',$data['data']);

        $this->smarty->assign('data',$fields);

        $this->smarty->assign('title',$form['title']);
        $this->smarty->assign('fid',$fid);

        $fields=explode(';',$form['content']);
        $paramArray=array();
        foreach($fields as $one){
            list($fname,$ftypeParam,$must,$append)=explode('+',$one);
            list($ftype,$fparam)=explode(':',$ftypeParam);
            $paramArray[]=array(
                'fname'=>$fname,
                'ftype'=>$ftype,
                'fparam'=>$fparam,
                'must'=>$must,
                'append'=>$append
            );
        }
        unset($fields);

        $this->smarty->assign('params',$paramArray);

        $this->smarty->assign('FIELD_TEXT',FormModel::FIELD_TEXT);
        $this->smarty->assign('FIELD_LONGTEXT',FormModel::FIELD_LONGTEXT);
        $this->smarty->assign('FIELD_NUM',FormModel::FIELD_NUM);
        $this->smarty->assign('FIELD_DATE_FUTURE',FormModel::FIELD_DATE_FUTURE);
        $this->smarty->assign('FIELD_DATE_PAST',FormModel::FIELD_DATE_PAST);
        $this->smarty->assign('FIELD_TIME',FormModel::FIELD_TIME);
        $this->smarty->assign('FIELD_PHOTO',FormModel::FIELD_PHOTO);
        $this->smarty->assign('FIELD_SINGLE',FormModel::FIELD_SINGLE);
        $this->smarty->assign('FIELD_MULTI',FormModel::FIELD_MULTI);
        $this->smarty->assign('FIELD_PHONE',FormModel::FIELD_PHONE);
        $this->smarty->assign('FIELD_EMAIL',FormModel::FIELD_EMAIL);
        $this->smarty->assign('FIELD_AREA',FormModel::FIELD_AREA);


        $this->smarty->display("tmp/form.html");
    }



    private $redpackId=42;

    /**
     * 红包领取页
     */
    public function redpack(){
        if(!$this->isWechat){
            die("请在微信中打开本链接");
        }
        $id=I('get.id',0,'int');
        if (!$id){
            die("错误的抽奖入口");
        }

        $set=RedpackModel::getPackInstance()->where("id={$id}")->find();
        if (!$set){
            die("抽奖活动并不存在！");
        }

        session('redpackId',$id);
        $this->redpackId=$id;
        $uid = session('uid');

        $now=time();
        if(RedpackModel::getChanceInstance()->where("uid={$uid} and setId={$this->redpackId} and type=0")->find()){
            //已经发过默认卡券
        }else{
            RedpackModel::getChanceInstance()->add(
                array('uid'=>$uid,'setId'=>$this->redpackId,'addTime'=>$now,'type'=>0,'status'=>0)
            );
        }


        $chanceTimes=RedpackModel::getChanceCount($uid,$this->redpackId,RedpackModel::CHANCE_STATUS_UNUSED);
        $this->smarty->assign('chanceTimes', $chanceTimes);

        $waveId=RedpackModel::getWaveInstance()->where("setId={$this->redpackId} and $now >= beginTime and endTime")->max('waveId');
        if(!$waveId){
            $waveId=0;
        }

        $list=$this->redpack_list($waveId);
        $set['rule']=str_replace("\n","<br />",$set['rule']);
        $this->smarty->assign('set', $set);
        $this->smarty->assign('userList', $list);
        //微信分享相关
        $this->smarty->assign('isWxShare', true);
        $this->smarty->assign('wxTitle', "丸子妈妈新年送红包");
        $this->smarty->assign('wxDesc', "最高抢66.6吉利包，先到先得，抢完截止");
        $this->smarty->assign('wxLink', 'http://wzma.176dp.cn/wap.php?m=Wap&c=Tmp&a=redpack&id='.$this->redpackId);
        $this->smarty->assign('wxImage', 'http://wzma.176dp.cn/Public/Wap/redpack/shareIcon.jpg');


        if(strcasecmp($set['limitZone'],'NONE')==0){//不限制
            $this->smarty->assign('limitZone', '0');
        }else{//限制
            $this->smarty->assign('limitZone', '1');
        }


        $this->smarty->display('tmp/redpack.html');
    }


    public function redpack_list($waveId){
        $this->redpackId=session('redpackId');
        $listStr= <<<END
一生承诺,1,获得2.8元
为你点赞,2,获得精灵总动员10元抵扣券
简单灬爱,3,获得1元
流年灬未亡,1,获得18.8元
舞动Dē灵魂￠,2,获得精灵总动员5元抵扣券
__没有背景丶只有背影,3,获得1元
乂日光倾城¤,1,获得精灵总动员10元抵扣券
丶猫猫er,2,获得1元
雪花ミ飞舞,3,获得6.6元
姬〆小溅,1,获得1.8元
涐们的幸福像流星丶,2,获得6.6元
吾皇万岁_万万岁,3,获得8.8元
倾听冷暖丿,1,获得精灵总动员5元抵扣券
﹡巴黎铁塔,2,获得6.6元
_倾月轩萱_,3,获得精灵总动员5元抵扣券
相见不如怀念〆,1,获得6.6元
沵算what°,2,获得1元
放手也是一种罪过‖,3,获得精灵总动员5元抵扣券
有妳很瞞促,1,获得1元
早已▲沧海桑田,2,获得2.8元
冷落了♂自己·,3,获得精灵总动员10元抵扣券
帶著面具過日子~,1,获得2.8元
體溫 ㎝ ╮,2,获得1.8元
海阔天空,3,获得66元
代价是折磨╳,1,获得2.8元
焚心劫,2,获得精灵总动员10元抵扣券
◆乱世梦红颜,3,获得1.8元
有你，很幸福,1,获得1.8元
哭花了素颜、,2,获得6.6元
昂贵的、背影,3,获得精灵总动员10元抵扣券
淡淡の、清香,1,获得6.6元
笑叹★尘世美,2,获得1.8元
似曾相识,3,获得1.8元
可可妈,1,获得1.8元
こ情ゼ街ツ,2,获得1元
一生承诺,3,获得1.8元
罗纳尔蛋,1,获得8.8元
简单灬爱,2,获得1.8元
流年灬未亡,3,获得精灵总动员5元抵扣券
舞动Dē灵魂￠,1,获得1.8元
__没有背景丶只有背影,2,获得8.8元
乂日光倾城¤,3,获得1元
丶猫猫er,1,获得精灵总动员10元抵扣券
雪花ミ飞舞,2,获得1元
姬〆小溅,3,获得2.8元
涐们的幸福像流星丶,1,获得1元
吾皇万岁_万万岁,1,获得2.8元
倾听冷暖丿,2,获得精灵总动员5元抵扣券
﹡巴黎铁塔,3,获得6.6元
倾月轩萱,1,获得2.8元
相见不如怀念〆,2,获得精灵总动员10元抵扣券
沵算what°,3,获得1.8元
放手也是一种罪过‖,1,获得1.8元
有妳很瞞促,2,获得6.6元
早已▲沧海桑田,3,获得1元
冷落了♂自己·,1,获得2.8元
帶著面具過日子~,2,获得2.8元
體溫 ㎝ ╮,3,获得2.8元
海阔天空,1,获得1元
代价是折磨╳,2,获得1.8元
焚心劫,3,获得1元
◆乱世梦红颜,1,获得精灵总动员10元抵扣券
有你，很幸福,2,获得2.8元
哭花了素颜、,3,获得精灵总动员5元抵扣券
昂贵的、背影,1,获得1元
淡淡の、清香,1,获得2.8元
笑叹★尘世美,2,获得1元
似曾相识,3,获得1元
可可妈,1,获得18.8元
こ情ゼ街ツ,2,获得66元
′A!伱.,3,获得18.8元
星杰(二),1,获得1.8元
愛/heart,2,获得8.8元
拽ご地皇ミ,3,获得1.8元
莲波仙子,1,获得1.8元
草莓味的吻,2,获得1.8元
一半惊喜一半遗憾,3,获得6.6元
热情消退,1,获得1.8元
依赖成折磨,2,获得8.8元
爱不单行,3,获得2.8元
爱到最后全是伤,1,获得1元
没人要的野猫,2,获得精灵总动员10元抵扣券
十里温柔,3,获得1.8元
安暖盛夏夜未央,1,获得18.8元
南葵思暖,1,获得精灵总动员5元抵扣券
华灯初上,2,获得2.8元
愿岁月可回头,3,获得1.8元
青空如璃,1,获得1元
刚好遇见你,2,获得精灵总动员5元抵扣券
繁花入梦,3,获得1元
待你如故,1,获得1元
如初〃,2,获得18.8元
栀夏,3,获得1元
一万里的晴空,1,获得2.8元
月光倾城,2,获得精灵总动员10元抵扣券
樱*^o^*🍒,3,获得6.6元
洛洛,1,获得精灵总动员5元抵扣券
END;
        $records=explode("\n",$listStr);
        $list=array();
        foreach ($records  as $k=>$v){
            list($nickname,$wave,$got)=explode(',',$v);
            if($wave<=$waveId){
                $list[]=UtilsModel::maskNickname($nickname).$got;
            }
        }
        shuffle($list);
        return $list;
    }




    

    /**
     * 打开红包
     */
    public function redpackOpen(){
        $this->redpackId=session('redpackId');
        $uid=session('uid');
        $nickname=session('nickname');
        $address=I('post.address','');


        $set=RedpackModel::getPackInstance()->where("id={$this->redpackId}")->find();
        if (!$set) {
            $this->ajaxReturn(array('status' => 2, 'msg' => '抽奖活动并不存在！'));
        }

        if(strcasecmp($set['limitZone'],RedpackModel::LIMIT_ZONE_NONE)!==0) {
            $addressKv = RedpackModel::getLimitZoneUseKV();
            if ($address=='' || strpos($address, $addressKv[$set['limitZone']]) === false) {
                $this->ajaxReturn(array('status' => 2, 'msg' => "本次活动仅限{$addressKv[$set['limitZone']]}的用户参加<br />您的位置是<{$address}>"));
            }
        }


        $this->ajaxReturn(RedpackModel::useChance($uid,$nickname,$this->redpackId));
    }

    /**
     * 分享回调处理
     */
    public function redpackShare(){//限获得
        $this->redpackId=session('redpackId');
        $uid=session('uid');
        if(!$uid){
            $this->ajaxReturn(array('status'=>1,'msg'=>'请先登陆'));
        }
        $this->ajaxReturn(RedpackModel::addChance($uid,$this->redpackId));
    }


    public function redpackPrize(){
        $this->redpackId=session('redpackId');
        $uid=session('uid');
        if(!$uid){
            $this->ajaxReturn(array('status'=>1,'msg'=>'请先登陆'));
        }
        $list=RedpackModel::getPrizeList($uid,$this->redpackId);
        $result='';
        foreach ($list['data'] as $one){
            $str =date('Y-m-d H:i:s',$one['sendTime']);
            switch($one['prizeType']){
                case RedpackModel::PRIZE_TYPE_MONEY:
                    $str.="&nbsp;&nbsp;获得现金红包".($one['money']/100).'元';
                    break;
                case RedpackModel::PRIZE_TYPE_POINT:
                    $str.="&nbsp;&nbsp;获得{$one['money']}积分";
                    break;
                case RedpackModel::PRIZE_TYPE_QUOTA:
                    $str.="&nbsp;&nbsp;获得{$one['prizeName']}";
                    break;
            }
            $result.=$str.'<br />';
        }
        if ($result==''){
            $result='您还没有获得奖励，继续加油吧！';
        }
        echo $result;
    }

}